<template>
  <div>{{errorMsg}}</div>
  <el-menu>
    <el-sub-menu class="el-sub-menu" v-for="(menu,index) in menuList" :index="index">
      <template #title>
        <span>{{ menu.title }}</span>
      </template>
      <template v-for="(child) in menu.children">
        <el-menu-item>
          <router-link :to="child.path">
            {{ child.title }}
          </router-link>
        </el-menu-item>
      </template>
    </el-sub-menu>
  </el-menu>
</template>

<script setup>
import {ref, reactive,onMounted, toRefs} from "vue";
import http from '@/axios/http';
import qs from "qs";
import {dynamicAddRoute} from "@/util/dynamicRoute"
import {useUserStore} from "@/store/userStore.js";
import router from "@/util/router.js";

let errorMsg = reactive("");
const menuList = ref([]);

const props = defineProps({
  roleid:Number
})

const {roleid} = toRefs(props);

onMounted(() => {
  init();
});

function init(){
  const userStore = useUserStore();
  console.log("user");
  console.log(userStore.getUserinfo);
  const param = reactive({
    roleid: userStore.getUserinfo.roleId
  });
  console.log("param",param)
  //查询菜单
  http.post('/lyy/get_menus', qs.stringify(param)).then((res) => {
    let resultmenu = res.data;

    if (resultmenu.result === "success") {
      menuList.value = resultmenu.data;
      console.log(menuList);
      dynamicAddRoute(menuList.value);
      if (menuList.value === null) {
        errorMsg = "获取菜单失败！";
      }
    } else {
      console.log(resultmenu.error)
      errorMsg = "获取菜单失败！";
      // router.push({path: '/login'});
    }
  })
}


</script>

<style scoped>

</style>